﻿//-----------------------------------------------------------------------
// <copyright file="ContentPage.cs" company="Craig Longford (deltawolf7@gmail.com)">
//     Copyright (c) Craig Longford. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Delta.Mercury.Business
{
    using System;
    using System.Data;
    using Delta.Data.DataExpress;
    using Delta.Mercury.Business.Exceptions;

    /// <summary>
    /// Content page
    /// </summary>
    [Serializable]
    public class ContentPage : IdentifierBase, IDataServicable
    {
        #region Members
        /// <summary>
        /// Meta desciption
        /// </summary>
        private string metaDescription;

        /// <summary>
        /// Meta title
        /// </summary>
        private string metaTitle = string.Empty;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the ContentPage class.
        /// </summary>
        public ContentPage()
        {
            this.MetaKeywords = new Delta.Mercury.Business.MetaKeywords();
            this.Robots = RobotTag.All;
            this.metaDescription = string.Empty;
            this.Searchable = true;
            this.ContentText = string.Empty;
            this.Enabled = true;
            this.PageName = "UNTITLED";
            this.PageTitle = string.Empty;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the content text
        /// </summary>
        public string ContentText { get; set; }

        /// <summary>
        /// Gets or sets the page description
        /// </summary>
        public string Description
        {
            get
            {
                return this.metaDescription;
            }

            set
            {
                if (value.Length > 200)
                {
                    throw new InvalidLengthException("Description element can't be greater than 200 characters, defined by the W3C standard.");
                }

                this.metaDescription = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the page is enabled or not
        /// </summary>
        public bool Enabled { get; set; }

        /// <summary>
        /// Gets or sets the meta keywords
        /// </summary>
        public MetaKeywords MetaKeywords { get; set; }

        /// <summary>
        /// Gets or sets the meta title
        /// </summary>
        public string MetaTitle
        {
            get
            {
                return this.metaTitle;
            }

            set
            {
                if (value.Length > 0x40)
                {
                    throw new InvalidLengthException("Title element can't be greater than 64 characters, defined by the W3C standard.");
                }

                this.metaTitle = value;
            }
        }

        /// <summary>
        /// Gets or sets the page name
        /// </summary>
        public string PageName { get; set; }

        /// <summary>
        /// Gets or sets the page title
        /// </summary>
        public string PageTitle { get; set; }

        /// <summary>
        /// Gets or sets the page robots
        /// </summary>
        public RobotTag Robots { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the page is searchable or not
        /// </summary>
        public bool Searchable { get; set; }
        #endregion

        #region Load Methods
        /// <summary>
        /// Load content page from datasource
        /// </summary>
        /// <param name="uniqueIdentifier">Unique identifier</param>
        /// <returns>Content page</returns>
        public static ContentPage Load(Guid uniqueIdentifier)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", uniqueIdentifier));
            return DataAccess.GetSingle<ContentPage>("LoadContentPage", CommandType.StoredProcedure, parameters);
        }

        /// <summary>
        /// Load content page from datasource
        /// </summary>
        /// <param name="pageName">Page name string</param>
        /// <returns>Content page</returns>
        public static ContentPage Load(string pageName)
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("PageName", pageName));
            return DataAccess.GetSingle<ContentPage>("LoadContentPage", CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Delete Method
        /// <summary>
        /// Delete page from database
        /// </summary>
        public void Delete()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", this.Guid));
            DataAccess.NonQuery("DeleteContentPage", CommandType.StoredProcedure, parameters);
        }
        #endregion

        #region Fill Method
        /// <summary>
        /// Fill object from database
        /// </summary>
        /// <param name="reader">Data reader</param>
        public void Fill(IDataReader reader)
        {
            this.Description = Helper.ToString(reader["Description"]);
            this.Guid = Helper.ToGuid(reader["Guid"]);
            this.MetaTitle = Helper.ToString(reader["Title"]);
            this.Robots = Helper.ToEnum<RobotTag>(reader["Robots"]);
            this.MetaKeywords = Delta.Mercury.Business.MetaKeywords.SetKeywords(Helper.ToString(reader["Keywords"]));
            this.Searchable = Helper.ToBool(reader["Searchable"]);
            this.ContentText = Helper.ToString(reader["ContentText"]);
            this.Enabled = Helper.ToBool(reader["Enabled"]);
            this.PageName = Helper.ToString(reader["PageName"]);
            this.PageTitle = Helper.ToString(reader["PageTitle"]);
        }
        #endregion   

        #region Save Method
        /// <summary>
        /// Save content page to data source
        /// </summary>
        public void Save()
        {
            Parameters parameters = new Parameters();
            parameters.Add(new Parameter("Guid", this.Guid));
            parameters.Add(new Parameter("Title", this.MetaTitle));
            parameters.Add(new Parameter("Description", this.Description));
            parameters.Add(new Parameter("Robots", this.Robots));
            parameters.Add(new Parameter("Keywords", this.MetaKeywords.GetKeywords()));
            parameters.Add(new Parameter("Searchable", this.Searchable));
            parameters.Add(new Parameter("ContentText", this.ContentText));
            parameters.Add(new Parameter("Enabled", this.Enabled));
            parameters.Add(new Parameter("PageName", this.PageName));
            parameters.Add(new Parameter("PageTitle", this.PageTitle));
            DataAccess.NonQuery("SaveContentPage", CommandType.StoredProcedure, parameters);
        }
        #endregion
    }
}

